﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Data
{
    public class EntityMetadata
    {
        public const string FieldId = "__Id";
        public const string FieldName = "__Name";
        public const string FieldCreator = "__Creator";
        public const string FieldCreatedDate = "__CreatedDate";

        public Guid Id { get; set; }

        public string Name { get; set; }

        public string Caption { get; set; }

        public List<PropertyMetadata> Properties { get; set; }

        #region 关联信息
        public List<EntityMetadata> Relations { get; set; }
        public EntityMetadata PrimaryEntity { get; set; }
        public Guid? PrimaryEntityId { get; set; }
        #endregion

        public EntityMetadata()
        {
        }

        private PropertyMetadata _identityProperty;
        private PropertyMetadata _nameProperty;
        private PropertyMetadata _creatorProperty;
        private PropertyMetadata _createdDateProperty;

        public PropertyMetadata GetIdentity()
        {
            if (_identityProperty == null)
            {
                foreach (PropertyMetadata property in Properties)
                {
                    if (property.IsIdentity)
                    {
                        _identityProperty = property;
                        break;
                    }
                }
            }

            if (_identityProperty == null)
            {
                throw new Exception("没有发现Identity属性!");
            }
            return _identityProperty;
        }
    }
}
